#!/usr/bin/env python3
#
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
"""HRW4U script - Process HRW4U input and produce output (AST or HRW)."""

from __future__ import annotations

from hrw4u.hrw4uLexer import hrw4uLexer
from hrw4u.hrw4uParser import hrw4uParser
from hrw4u.visitor import HRW4UVisitor
from hrw4u.common import create_base_parser, create_parse_tree, generate_output, process_input


def main() -> None:
    """Main entry point for the hrw4u script."""
    parser, output_group = create_base_parser("Process HRW4U input and produce output (AST or HRW).")

    # Argument parsing
    output_group.add_argument("--hrw", action="store_true", help="Produce the HRW output (default)")
    parser.add_argument("--no-comments", action="store_true", help="Skip comment preservation (ignore comments in output)")
    args = parser.parse_args()

    # Default to HRW output if neither AST nor HRW specified
    if not (args.ast or args.hrw):
        args.hrw = True

    content, filename = process_input(args.input_file)
    tree, parser_obj, error_collector = create_parse_tree(
        content, filename, hrw4uLexer, hrw4uParser, "hrw4u", not args.stop_on_error)

    # Generate output
    generate_output(tree, parser_obj, HRW4UVisitor, filename, args, error_collector)


if __name__ == "__main__":
    main()
